package com.foxzzz.greedy;


import java.util.LinkedList;
import java.util.List;

public class Solution860 {

    public static void main(String[] args) {
        System.out.println(new Solution860().lemonadeChange(new int[]{5, 5, 5, 10, 20}));
        System.out.println(new Solution860().lemonadeChange(new int[]{5, 5, 10}));
        System.out.println(new Solution860().lemonadeChange(new int[]{10, 10}));
        System.out.println(new Solution860().lemonadeChange(new int[]{5, 5, 10, 10, 20}));
        System.out.println(new Solution860().lemonadeChange(new int[]{5, 5, 10, 20, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 5, 5, 20, 5, 20, 5}));
    }

    public boolean lemonadeChange(int[] bills) {
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < bills.length; i++) {
            if (bills[i] > 5) {
                if (!list.remove(Integer.valueOf(5))) {
                    return false;
                }
                if (bills[i] == 20) {

                    if (!list.remove(Integer.valueOf(10))) {
                        if(!(list.remove(Integer.valueOf(5)) && list.remove(Integer.valueOf(5)))){
                        return false;
                        }
                    }

                }

            }
            list.add(bills[i]);
        }
        return true;
    }
}
